﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace MySSO.Core
{
    public class DecryptString
    {
        public const string BASE_KEY = "SHUTDOWN";
        public static byte[] HexStringToBytes(string hexString)
        {
            if (hexString == null)
            {
                throw new ArgumentNullException("hexString");
            }

            if ((hexString.Length & 1) != 0)
            {
                throw new ArgumentOutOfRangeException("hexString", hexString, "hexString must contain an even number of characters.");
            }

            byte[] result = new byte[hexString.Length / 2];

            for (int i = 0; i < hexString.Length; i += 2)
            {
                result[i / 2] = byte.Parse(hexString.Substring(i, 2), NumberStyles.HexNumber);
            }

            return result;
        }

        public static string BytesToHexString(byte[] bytes)
        {
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bytes)
            {
                s.Append(b.ToString("x2").ToUpper());
            }
            return s.ToString();
        }

        /// <summary>
        /// DES 解密算法
        /// </summary>
        /// <param name="Data">密文</param>
        /// <param name="key">密钥长度为8个字符</param>
        /// <param name="charset">字符编码</param>
        /// <returns>明文</returns>
        public static string DESDecrypt(string Data, string key, string charset)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            des.Key = ASCIIEncoding.ASCII.GetBytes(key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(key);

            byte[] inputByteArray = HexStringToBytes(Data);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();


            byte[] ret = ms.ToArray();

            cs.Close();
            ms.Close();

            return Encoding.GetEncoding(charset).GetString(ret);
        }

        /// <summary>
        /// DES加密算法
        /// </summary>
        /// <param name="Data">加密明文</param>
        /// <param name="key">密钥长度为8个字符</param>
        /// <param name="charset">字符编码</param>
        /// <returns>返回密文</returns>
        public static string DESEncrypt(string Data, string key, string charset)
        {

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            des.Key = ASCIIEncoding.ASCII.GetBytes(key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(key);

            byte[] inputByteArray = Encoding.GetEncoding(charset).GetBytes(Data);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);

            cs.FlushFinalBlock();

            byte[] ret = ms.ToArray();

            cs.Close();
            ms.Close();

            return BytesToHexString(ret);
        }

        public static string DESDecrypt(string Data, string key)
        {
            return DESDecrypt(Data, key, "utf-8");
        }
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static string DESDecrypt(string Data)
        {
            return DESDecrypt(Data, BASE_KEY);
        }



        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="Data">明文</param>
        /// <param name="key">秘钥8位数</param>
        /// <returns></returns>
        public static string DESEncrypt(string Data, string key)
        {
            return DESEncrypt(Data, key, "utf-8");
        }

        /// <summary>
        ///  DES加密
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static string DESEncrypt(string Data)
        {
            return DESEncrypt(Data, BASE_KEY);
        }

        #region  md5

        /** 获取大写的MD5签名结果 */
        public static string MD5(string encypStr, string charset)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = Encoding.GetEncoding(charset).GetBytes(encypStr);
            bs = md5.ComputeHash(bs);
            return BytesToHexString(bs);
        }

        public static string MD5(string encypStr)
        {
            return MD5(encypStr, "utf-8");
        }


        /// <summary>
        /// SHA256 加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SHA256(string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            HashAlgorithm algorithm = null;
            algorithm = new SHA256Managed();
            return BitConverter.ToString(algorithm.ComputeHash(bytes)).Replace("-", "").ToUpper();
        }
        #endregion 
    }
}
